/**
 * @author 徐楠
 * @date 2021/12/17 19:35
 * @version 1.0
 */

package com.xunan.likou;

public class AddTwoNumbers {

    static class ListNode {
        int val;
        ListNode next;

        public ListNode() {
        }

        public ListNode(int x) {
            val = x;
        }
    }

    public static void main(String[] args) {
        ListNode a = new ListNode(2);
        ListNode b = new ListNode(5);
        ListNode aStart = a;
        ListNode bStart = b;

        ListNode c1 = new ListNode(4);
        a.next = c1;
        a = a.next;
        ListNode c2 = new ListNode(3);
        a.next = c2;
        a = a.next;

        ListNode d1 = new ListNode(6);
        b.next = d1;
        b = b.next;
        ListNode d2 = new ListNode(4);
        b.next = d2;
        b = b.next;

        a= aStart;
        b = bStart;
        ListNode listNode = addTwoNumbers(a, b);

        System.out.println();
        System.out.println("********");
        while(listNode != null){
            System.out.println(listNode.val);
            listNode = listNode.next;
        }



    }

    /**
     * 采用向前进位的加法
     *
     * @param l1
     * @param l2
     * @return
     */
    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {

        int a, b;
        int flag = 0;
        ListNode result = new ListNode();
        ListNode resultNext = result;

        while (l1 != null || l2 != null || flag == 1) {
            if (l1 != null && l2 != null) {
                a = l1.val;
                b = l2.val;
                resultNext.val = a + b + flag >= 10 ? (a + b + flag - 10) : (a + b + flag);
                if (a + b + flag >= 10) {
                    flag = 1;
                } else {
                    flag = 0;
                }
                l1 = l1.next;
                l2 = l2.next;
            } else if (l1 != null && l2 == null) {
                a = l1.val;
                resultNext.val = a + flag >= 10 ? (a + flag - 10) : (a + flag);
                if (a + flag >= 10) {
                    flag = 1;
                } else {
                    flag = 0;
                }
                l1 = l1.next;
            } else if (l1 == null && l2 != null) {
                b = l2.val;
                resultNext.val = b + flag >= 10 ? (b + flag - 10) : (b + flag);
                if (b + flag >= 10) {
                    flag = 1;
                } else {
                    flag = 0;
                }
                l2 = l2.next;
            } else if (l1 == null && l2 == null) {
                //相当于没值了，但是有进位即flag = 1
                resultNext.val = 1;
                flag = 0;
            }

            if (l1 == null && l2 == null && flag == 0) {
                System.out.println("''''");
                System.out.println("''''"+resultNext.val);
                resultNext= result;
                while(resultNext != null){
                    System.out.println(resultNext.val);
                    resultNext = resultNext.next;
                }
                System.out.println("''''");
                return result;
            }
            System.out.println("-"+resultNext.val);
            resultNext.next = new ListNode();
            resultNext = resultNext.next;
            System.out.println("+"+resultNext.val);
        }
        System.out.println("-+-"+resultNext.val);
        return result;

    }
}
